/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package formula.one.urjc.circuito.view;

import formula.one.urjc.Metodos;
import formula.one.urjc.circuito.Circuito;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author santiago
 */
public class BajaCircuito extends javax.swing.JInternalFrame {
    
    //VARIABLES GLOBALES
    //Declaramos este objeto para ejecutar los metodos genericos
    private Metodos metodo = new Metodos() ;
    private ArrayList<Object> arrayCircuitos;
    private int longitudArray;    
    final static String NOMBRE_FICHERO = "circuitos.dat" ; 

    /**
     * Creates new form BajaCircuito
     */
    public BajaCircuito() {
        initComponents();
        arrayCircuitos = metodo.getCircuitoList() ;
        longitudArray = arrayCircuitos.size();
        cargarCircuitos() ;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        circuitos = new javax.swing.JComboBox();
        aceptarBaja = new javax.swing.JButton();
        cancelarBaja = new javax.swing.JButton();

        jLabel1.setText("Baja Circuito");

        jLabel2.setText("Circuito:");

        circuitos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                circuitosActionPerformed(evt);
            }
        });

        aceptarBaja.setText("Aceptar");
        aceptarBaja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aceptarBajaActionPerformed(evt);
            }
        });

        cancelarBaja.setText("Cancelar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(67, 67, 67)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(circuitos, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(103, 103, 103)
                        .addComponent(aceptarBaja)
                        .addGap(52, 52, 52)
                        .addComponent(cancelarBaja))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(153, 153, 153)
                        .addComponent(jLabel1)))
                .addContainerGap(74, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(jLabel1)
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(circuitos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(61, 61, 61)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(aceptarBaja)
                    .addComponent(cancelarBaja))
                .addContainerGap(80, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void circuitosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_circuitosActionPerformed
        // TODO add your handling code here:
       
    }//GEN-LAST:event_circuitosActionPerformed

    private void aceptarBajaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aceptarBajaActionPerformed
        // Obtenemos el Nombre del Circuito seleccionado
        String buscarCircuito = (String) circuitos.getSelectedItem() ;
        // Recuperamos los valoras para cada Circuito
        boolean encontrado = false ;
        int j = 0 ;
        while (j <= longitudArray && !encontrado ) {        
        //for (int j = 0; j < longitudArray; j++) {
            Circuito circuitoLista = (Circuito) arrayCircuitos.get(j) ;
            String nombre_Circuito = circuitoLista.getNombre() ;
            encontrado = nombre_Circuito.equals(buscarCircuito) ;
            if (encontrado) {
                System.out.println("Encontrado Borrado") ;
                System.out.println("Antes de array: " + arrayCircuitos.size()) ;
                arrayCircuitos.remove(j) ;
                System.out.println("Despues de array: " + arrayCircuitos.size()) ;
                metodo.borrarFichero(NOMBRE_FICHERO) ;
                metodo.escribirFichero(NOMBRE_FICHERO, arrayCircuitos) ;
            }
            j++ ;
        }        
       cargarCircuitos() ;
    }//GEN-LAST:event_aceptarBajaActionPerformed

    private void cargarCircuitos() {
        circuitos.removeAllItems() ;
        System.out.println("REGISTROS EN EL FICHERO CARGAR COMBOS");
        circuitos.addItem("");
        arrayCircuitos = metodo.getCircuitoList() ;
        longitudArray=arrayCircuitos.size();
        for (int i = 0; i < longitudArray; i++) {
            System.out.println("Registro: " + arrayCircuitos.get(i));
            Circuito circuitoCombo = (Circuito) arrayCircuitos.get(i);
            circuitos.addItem(circuitoCombo.getNombre());
        }
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aceptarBaja;
    private javax.swing.JButton cancelarBaja;
    private javax.swing.JComboBox circuitos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables
}
